package per.hx.bs.web.yw.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.constant.Setting;
import per.hx.bs.entity.yw.YwLeaveDO;
import per.hx.bs.util.PageUtils;
import per.hx.bs.util.Query;
import per.hx.bs.web.sys.service.SysDictService;
import per.hx.bs.web.yw.service.YwLeaveService;

import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/4/20  10:05
 */
@Controller
@RequestMapping(value = "yw/leave")
public class LeaveController extends BaseController {

    private final String prefix = "yw/leave/";

    private final
    YwLeaveService leaveService;

    private final SysDictService dictService;

    @Autowired
    public LeaveController(YwLeaveService leaveService, SysDictService dictService) {
        this.leaveService = leaveService;
        this.dictService = dictService;
    }

    @BsCLog("进入请假审核界面")
    @RequiresPermissions("yw:leave:leave")
    @GetMapping(value = {"leave", "/"})
    public String leave() {
        return prefix + "leave";
    }

    @BsCLog("进入请假添加界面")
    @RequiresPermissions("yw:leave:add")
    @GetMapping(value = "/add")
    public String add() {
        return prefix + "add";
    }

    @BsCLog("进入请假审核界面")
    @RequiresPermissions("yw:leave:edit")
    @GetMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        YwLeaveDO ywLeaveDO = leaveService.selectOne(id);
        System.out.println(ywLeaveDO);
        model.addAttribute("leave", ywLeaveDO);
        return prefix + "edit";
    }

    @BsCLog("获取请假类型类型")
    @GetMapping(value = "/leaveType")
    @ResponseBody
    public ReturnDTO leaveType() {
        return ReturnDTO.ok(dictService.listByType(Setting.LEAVE_TYPE));
    }

    @BsCLog("获取请假审核类型")
    @GetMapping(value = "/leaveSHType")
    @ResponseBody
    public ReturnDTO leaveSHType() {
        return ReturnDTO.ok(dictService.listByType(Setting.LEAVE_SH_TYPE));
    }

    @BsCLog("获取请假信息")
    @RequiresPermissions("yw:leave:leave")
    @GetMapping(value = "/leave/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<YwLeaveDO> sysUserList = leaveService.leaveList(query);
        int total = leaveService.leaveCount(query);
        return new PageUtils(sysUserList, total);
    }

    @BsCLog("请假添加")
    @PostMapping(value = "/add")
    @ResponseBody
    public ReturnDTO add(YwLeaveDO ywLeaveDO) {
        ywLeaveDO.setCreateId(getUserId());
        System.out.println(ywLeaveDO);
        return ReturnDTO.check(leaveService.insert(ywLeaveDO).intValue());
    }

    @BsCLog("请假审核")
    @PostMapping(value = "/edit")
    @ResponseBody
    public ReturnDTO edit(YwLeaveDO ywLeaveDO) {
        ywLeaveDO.setUpdateId(getUserId());
        ywLeaveDO.setAgreeUser(getUserId());
        ywLeaveDO.setAgreeUserName(getName());
        return ReturnDTO.check(leaveService.update(ywLeaveDO));
    }

    @BsCLog("删除请假审核")
    @GetMapping(value = "/remove/{id}")
    @ResponseBody
    public ReturnDTO remove(@PathVariable("id") Long id) {
        return ReturnDTO.check(leaveService.delete(id));
    }
}
